package wk.demo.bean;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 *  获取一个文件夹下所有的文件。
 */
public class DirFiles {
    private List<FileItem> mFileList = new ArrayList<>();

    public DirFiles( File parentDirectory ) {

        if (parentDirectory.getParent() != null  ) {
            FileItem parentFile = new FileItem( new File(parentDirectory, "..") ,true);
            mFileList.add(parentFile);
        }

        File[] fileList = parentDirectory.listFiles();

        if(fileList==null){
            return;
        }


        for (File file : fileList) {
            //  不显示 以.开头的文件和文件夹
            if( file.getName().startsWith(".")){
                continue;
            }
            mFileList.add( new FileItem( file  ,file.isDirectory() ));
        }

        Collections.sort( mFileList,  new Comparator<FileItem>() {
            @Override
            public int compare(FileItem lhs, FileItem rhs) {
                if (lhs.isDirectory() && !rhs.isDirectory())
                    return -1;
                else if (!lhs.isDirectory() && rhs.isDirectory())
                    return 1;
                return lhs.getFile().getName().compareToIgnoreCase(rhs.getFile().getName());
            }
        });

    }

    public List<FileItem> getmFileList() {
        return mFileList;
    }


}
